package com.info.zhiduoduo.repository.mapper.memorandum;

import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.info.zhiduoduo.repository.dto.memorandum.MemorandumListDto;
import com.info.zhiduoduo.repository.entity.memorandum.PlatformMemorandum;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author cuichenglong
 * @since 2020-10-22
 */
public interface PlatformMemorandumMapper extends BaseMapper<PlatformMemorandum> {

    @Select(" SELECT pm.* FROM zdd_platform_memorandum pm " +
            " WHERE IF(ISNULL(#{keyWord}) OR #{keyWord} = '', 1=1, " +
            " pm.customer_name like '%${keyWord}%' " +
            " OR pm.title like '%${keyWord}%' OR pm.content like '%${keyWord}%') " +
            " AND IF(#{type} = 0, 1=1, pm.type = #{type}) " +
            " AND IF(#{customerType} = 0, 1=1, pm.customer_type = #{customerType}) " +
            " ORDER BY pm.state ASC, pm.createtime desc ")
    List<MemorandumListDto> queryMemorandumByCondition(String keyWord, int type, int customerType);

    @Select("SELECT content  " +
            "FROM zdd_platform_memorandum " +
            "WHERE type in(6,7) " +
            "AND state = 2 " +
            "AND customer_id = #{customerId} " +
            "AND customer_type = #{customerType} " +
            "ORDER BY createtime DESC  " +
            " LIMIT 1 ")
    String getSpecialNeedsContent(@Param("customerId") int customerId,
                                  @Param("customerType") int customerType);

    @Select("SELECT type  " +
            "FROM zdd_platform_memorandum " +
            "WHERE type = 7 " +
            "AND state = 2 " +
            "AND customer_id = #{customerId} " +
            "AND customer_type = #{customerType} " +
            "ORDER BY createtime DESC  " +
            "LIMIT 1 ")
    Integer getType(@Param("customerId") int customerId,
                    @Param("customerType") int customerType);

    @Select("SELECT " +
            " sum(free_years) " +
            "FROM " +
            " `zdd_platform_memorandum`  " +
            "WHERE " +
            " `customer_type` = #{customerType}  " +
            " AND `customer_id` = #{customerId}  " +
            " AND `free_type` = 2  " +
            " AND `state` = 2  " +
            " AND `type` = 4")
    int getFreeYears(@Param("customerId") int customerId,
                     @Param("customerType") int customerType);

}
